define(function(require, exports, module) {

	var $ = require('jquery');
	require('jbox');
	require('../util/tinymce-admin-page.js');	
	require('jquery/jquery-form/1.0.0/jquery.form.js');
	require('jquery/jquery-file-upload/8.8.5/vendor/jquery.ui.widget.js');
	require('jquery/jquery-file-upload/8.8.5/jquery.iframe-transport.js');
	require('jquery/jquery-file-upload/8.8.5/jquery.fileupload.js');

	var info = '';

	//加载
	var url = $CONFIG.base_url + '/api/configure/get';
	$.get(url,function(data){
		info = data;
		$("#img-preview").append('<img src="' + $CONFIG.base_url + 
			'/common/attachment/download/' + info.loginId + '/125x125"/>')
	});

	//加载服务描述
	tinymce.init({
			selector : 'textarea#projectdesc',
			theme : "modern",
			plugins : [ "preview image table contextmenu",
			            "emoticons paste textcolor"],
			            toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
			            toolbar2: "print preview media | forecolor backcolor emoticons",
			language : 'zh_CN',
			height:'200px',
			file_browser_callback : function(field_name, url, type, win) {
				if (type == 'image') {
					$('#fileupload').click();
				}
			},
			init_instance_callback:load_prodesc,
		});
	function load_prodesc(inst){
		$("#projectdesc_ifr").contents().find("#tinymce").html(info.projectDescription);
	}

	//加载网页底部信息
	tinymce.init({
			selector : 'textarea#footerinfo',
			theme : "modern",
			plugins : [ "preview image table contextmenu",
			            "emoticons paste textcolor"],
			            toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
			            toolbar2: "print preview media | forecolor backcolor emoticons",
			language : 'zh_CN',
			height:'200px',
			file_browser_callback : function(field_name, url, type, win) {
				if (type == 'image') {
					$('#fileupload').click();
				}
			},
			init_instance_callback:load_footerinfo,
		});
	function load_footerinfo(inst){
		$("#footerinfo_ifr").contents().find("#tinymce").html(info.footDescription);
	}

	//加载网页关键字
	tinymce.init({
			selector : 'textarea#webkeyword',
			theme : "modern",
			plugins : [ "preview image table contextmenu",
			            "emoticons paste textcolor"],
			            toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
			            toolbar2: "print preview media | forecolor backcolor emoticons",
			language : 'zh_CN',
			height:'100px',
			file_browser_callback : function(field_name, url, type, win) {
				if (type == 'image') {
					$('#fileupload').click();
				}
			},
			init_instance_callback:load_webkeyword,
		});
		function load_webkeyword(inst){
			$("#webkeyword_ifr").contents().find("#tinymce").html(info.webKeyword);
		}

	//加载网站描述
	tinymce.init({
			selector : 'textarea#webdesc',
			theme : "modern",
			plugins : [ "preview image table contextmenu",
			            "emoticons paste textcolor"],
			            toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
			            toolbar2: "print preview media | forecolor backcolor emoticons",
			language : 'zh_CN',
			height:'100px',
			file_browser_callback : function(field_name, url, type, win) {
				if (type == 'image') {
					$('#fileupload').click();
				}
			},
			init_instance_callback:load_webdesc,
		});
		function load_webdesc(inst){
			$("#webdesc_ifr").contents().find("#tinymce").html(info.webDescription);
		}

	// 上传图片
	exports.fileUpload = function fileUpload(e, v, a, s) {
		$("#" + a).fileupload(
				{
					url : $CONFIG.base_url + e,
					dataType : 'json',
					formData : {
						uid : g_user.id
					},
				 add: function (e, data) {
				        var goUpload = true;
				        var uploadFile = data.files[0];
				        if (!(/\.(gif|jpg|jpeg|png)$/i).test(uploadFile.name)) {
				        	$.jBox.tip('亲~请选择正确的图片格式');
				            goUpload = false;
				        }
				        if (uploadFile.size > 1048576) { // 1mb
				        	$.jBox.tip('亲~图片太大了，最大不超过1mb哦！');
				            goUpload = false;
				        }
				        if (goUpload == true) {
				            data.submit();
				        }
				    },
				    progressall : function(e, data) {
						var progress = parseInt(data.loaded / data.total * 100,10);
						$("#loading-load").show();
						$("#loading-load").text(progress + '%');
					},
					done : function(e, data) {
						 var result = data.result[0].body ? data.result[0].body.innerHTML : data.result;
						$.each(result, function(index, file) {
							var html = '<img src="' + $CONFIG.base_url
									+ '/common/attachment/download/' + file.id
									+ '/250x250"/>';
							if (s == "file-input") {
								$("#img-preview").empty().append(html);
								$('#' + v).val(file.id);
								$('.' + v + "-name").text(file.fileName);
								$("#loading-load").hide();
							} else {
								$('#' + s).empty().append(html);
								$('#' + v).val(file.id);
								$("#loading-load").hide();
							}
						});
					}
				});
	};

	
	$("#file-input").click(function() {
		v = "login-img";
		a = $(this).attr("id");
		s = "file-input";
		exports.fileUpload("/common/attachment/upload/multi", v, a, s);
	});


	//更新
	$("#save1").click(function(){
		var projectdesc = tinyMCE.get('projectdesc').getContent();
		$.ajax({
			url: $CONFIG.base_url + '/api/configure/update',
			type:'POST',
			data:{'projectDescription':projectdesc},
			success : function(data) {
					if(data)
					$.jBox.tip('更新成功~');
				else
					$.jBox.tip('更新失败~');
			},
			error : function() {
				$.jBox.tip('亲~出问题啦，请再试一次！');
			}
		})

	})

	$("#save2").click(function(){
		var footerinfo = tinyMCE.get('footerinfo').getContent();
		$.ajax({
			url: $CONFIG.base_url + '/api/configure/update',
			type:'POST',
			data:{'footDescription':footerinfo},
			success : function(data) {
					if(data)
					$.jBox.tip('更新成功~');
				else
					$.jBox.tip('更新失败~');
			},
			error : function() {
				$.jBox.tip('亲~出问题啦，请再试一次！');
			}
		})

	})

	$("#save3").click(function(){
		var webkeyword = tinyMCE.get('webkeyword').getContent();
		$.ajax({
			url: $CONFIG.base_url + '/api/configure/update',
			type:'POST',
			data:{'webKeyword':webkeyword},
			success : function(data) {
					if(data)
					$.jBox.tip('更新成功~');
				else
					$.jBox.tip('更新失败~');
			},
			error : function() {
				$.jBox.tip('亲~出问题啦，请再试一次！');
			}
		})

	})

	$("#save4").click(function(){
		var imgId = $("#login-img").val();
		$.ajax({
			url: $CONFIG.base_url + '/api/configure/update',
			type:'POST',
			data:{'loginId':imgId},
			success : function(data) {
					if(data)
					$.jBox.tip('更新成功~');
				else
					$.jBox.tip('更新失败~');
			},
			error : function() {
				$.jBox.tip('亲~出问题啦，请再试一次！');
			}
		})
	})
	$("#save5").click(function(){
		var postData = form2js("header-form",'.',true);
		//console.log(postData);
		$.ajax({
			url: $CONFIG.base_url + '/api/configure/update',
			type:'POST',
			data:{'indexItem1':postData.indexItem1,
				  'indexItem2':postData.indexItem2,
				  'indexItem3':postData.indexItem3},
			success : function(data) {
					if(data)
					$.jBox.tip('更新成功~');
				else
					$.jBox.tip('更新失败~');
			},
			error : function() {
				$.jBox.tip('亲~出问题啦，请再试一次！');
			}
		})
	})
	$("#save6").click(function(){
		var postData = form2js("recommend-form",'.',true);
		//console.log(postData);
		$.ajax({
			url: $CONFIG.base_url + '/api/configure/update',
			type:'POST',
			data:{'itemFirstname':postData.itemFirstname,
				  'itemSecname':postData.itemSecname,
				  'itemFname':postData.itemFname},
			success : function(data) {
					if(data)
					$.jBox.tip('更新成功~');
				else
					$.jBox.tip('更新失败~');
			},
			error : function() {
				$.jBox.tip('亲~出问题啦，请再试一次！');
			}
		})
	})

	$("#save7").click(function(){
		var webdesc = tinyMCE.get('webdesc').getContent();
		$.ajax({
			url: $CONFIG.base_url + '/api/configure/update',
			type:'POST',
			data:{'webDescription':webdesc},
			success : function(data) {
					if(data)
					$.jBox.tip('更新成功~');
				else
					$.jBox.tip('更新失败~');
			},
			error : function() {
				$.jBox.tip('亲~出问题啦，请再试一次！');
			}
		})

	})
	$("#save8").click(function(){
		$("#footer").find("input").each(function(){
			var text = $(this).val();
			$(this).parent().empty().append('<span>'+text+'</span>');
		});
		var footer = $("#footer").html();
		$.ajax({
			url: $CONFIG.base_url + '/api/configure/update',
			type:'POST',
			data:{'footDescription':footer},
			success : function(data) {
					if(data)
					$.jBox.tip('更新成功~');
				else
					$.jBox.tip('更新失败~');
			},
			error : function() {
				$.jBox.tip('亲~出问题啦，请再试一次！');
			}
		})

	})

})